Skip to content

Conversation

@popotec
Copy link

@popotec popotec commented Sep 18, 2024

I have encountered an issue in my Spring Batch job where, intermittently, the error "Singleton bean creation not allowed while the singletons of this factory are in destruction" occurs with the StatsdMeterRegistry bean.

This issue seems to arise when the application unexpectedly receives a SIGTERM signal (such as when a node goes down in a cloud environment) and begins the shutdown process, some background tasks in the StatsdMeterRegistry are not properly terminated before the bean destruction phase. As a result, there appears to be an attempt to access the StatsdMeterRegistry after the ApplicationContext has already started to shut down.

To address this, I believe it would be beneficial to gracefully shut down the StatsdMeterRegistry before the bean is destroyed. A possible solution could be to modify the StatsdMetricsExportAutoConfiguration to ensure that StatsdMeterRegistry.close() is called during the shutdown phase, as shown below:

    @PreDestroy
    public void destroy(StatsdMeterRegistry statsdMeterRegistry) {
        statsdMeterRegistry.close();
    }

This would help to properly terminate any ongoing background tasks and prevent the issue from occurring during application shutdown.

Thank you for considering this PR. I look forward to your feedback!

@pivotal-cla
Copy link

@popotec Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-cla
Copy link

@popotec Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 18, 2024
public void destroy(StatsdMeterRegistry statsdMeterRegistry) {
statsdMeterRegistry.close();
}
}
Copy link
Contributor

@mdeinum mdeinum Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work as @PreDestroy should not have any arguments (See https://jakarta.ee/specifications/platform/9/apidocs/jakarta/annotation/predestroy).

Finally as the StatsdMeterRegistry has a close method that should already be registered automatically as a pre-destroy method. For this see the @Bean.

So the solution to the problem lies not in adding additional annotations but probably elsewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @mdeinum. I was just going to comment with the same analysis.

@popotec can you please open an issue so that we can investigate further? Seeing the stack trace of the exception when bean creation is attempted during shutdown may be enough for us to figure out the cause.

@wilkinsona
Copy link
Member

Thanks for the PR, @popotec, but as discussed I don't think this is the right solution to your problem. Please do open an issue if you'd like us to investigate further.

@wilkinsona wilkinsona closed this Sep 18, 2024
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: declined A suggestion or change that we don't feel we should currently apply

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants